package com.wlz.util;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wlz.entity.request.f1.EcuVer;
import com.wlz.entity.request.f1.F1Request;
import com.wlz.entity.request.f1.Header;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Sheet;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author wlz
 * @date 2022-11-25  11:35 下午
 */
@Slf4j
public class ExcelUtils {

    public static void main(String[] args) {
        String fileUrl = "/Users/wlz/Desktop/618台车_OTA平台上报信息.xlsx";
        ExcelReader reader = ExcelUtil.getReader(FileUtil.file(fileUrl));
        log.info("reader: {}", reader);
        List<F1Request> f1Request = getF1Request(reader);
        for (F1Request request : f1Request) {
            String requestJson = JSONObject.toJSONString(request);
            log.info("requestJson: {}", requestJson);
        }

//        List<String> vinList = getVinList(reader);
//        log.info("vinList size: {}", vinList.size());
//        String s = JSONObject.toJSONString(vinList);
//        log.info("vinList {}", s);
    }

    public static List<F1Request> getF1Request(String fileUrl) {
        ExcelReader reader = ExcelUtil.getReader(FileUtil.file(fileUrl));
        List<F1Request> f1Request = getF1Request(reader);
        reader.close();
       return  f1Request;
    }


    public static List<F1Request> getF1Request(ExcelReader reader) {
        List<Sheet> sheets = reader.getSheets();
        List<F1Request> requests = new ArrayList<>();
        for (Sheet sheet : sheets) {
            String sheetName = sheet.getSheetName();
            log.info("sheetName: {}", sheetName);
            reader.setSheet(sheet);
            List<Map<String, Object>> maps = reader.readAll();
            log.info("maps:{}", maps.size());
            for (Map<String, Object> map : maps) {
                F1Request request = new F1Request();
                Header header = new Header();
                List<EcuVer> Ecus = new ArrayList<>();
                for (Map.Entry<String, Object> entry : map.entrySet()) {
                    String key = entry.getKey();
                    String value = (String) entry.getValue();
//                    log.info("key = {}, value = {}", key, value);
                    if ("VIN码".equalsIgnoreCase(key)) {
                        header.setVIN(value.trim());
                    }else if ("大版本号".equalsIgnoreCase(key)) {
                        header.setVer(value);
                    }
                    header.setReqMode(3);
                    header.setUDate(DateUtil.format(LocalDateTime.now(), DatePattern.PURE_DATETIME_PATTERN));
                    request.setHeader(header);
                    if (!"VIN码".equalsIgnoreCase(key) && !"整车物料号".equalsIgnoreCase(key) && !"大版本号".equalsIgnoreCase(key)
                    && !"最后上报时间".equalsIgnoreCase(key)) {
                        if (ObjectUtil.isNotEmpty(key) && ObjectUtil.isNotEmpty(value)) {
                            EcuVer ecuVer = new EcuVer();
                            ecuVer.setNa(key.trim());
                            String[] split = value.split("/");
                            ecuVer.setPn(split[0]);
                            ecuVer.setSv(split[1]);
                            Ecus.add(ecuVer);
                        }
                    }
                }
                if (ObjectUtil.isNotEmpty(header.getVIN())) {
                    request.setHeader(header);
                    request.setEcus(Ecus);
                    requests.add(request);
                }
            }
        }
        return requests;
    }


    public static List<String> getVins(String fileUrl) {
        ExcelReader reader = ExcelUtil.getReader(FileUtil.file(fileUrl));
        List<String> vinList = getVinList(reader);
        reader.close();
        return  vinList;
    }


    /**
     *  获取 vinList
     * @param reader
     * @return
     */
    public static List<String> getVinList(ExcelReader reader) {
        List<Sheet> sheets = reader.getSheets();
        List<String> list  = new ArrayList<>();
        for (Sheet sheet : sheets) {
            String sheetName = sheet.getSheetName();
            log.info("sheetName: {}", sheetName);
            reader.setSheet(sheet);
            List<Map<String, Object>> maps = reader.readAll();
            log.info("maps:{}", maps.size());
            for (Map<String, Object> map : maps) {
                list.add((String) map.get("VIN码"));
            }
        }
        return list;
    }

}
